﻿using UnityEngine;
using System.Net;
using System.IO;
using System.Text;

/// <summary>
/// Request 请求
/// Response 相应
/// </summary>
public class TeachHttpWebRequest : MonoBehaviour {
    
    string url = "http://cgi.im.qq.com/cgi-bin/cgi_svrtime";
    HttpWebRequest request;
    void Start_Get()
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "GET";//区分大小写

        WebResponse response = request.GetResponse();

        Stream stream = response.GetResponseStream();//一直等待全部加载完
        StreamReader reader = new StreamReader(stream, Encoding.UTF8);
        string buff = reader.ReadToEnd();
        Debug.Log(buff);

        response.Close();
    }




    void Start_Post()
    {
        request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";


        string form = "username=123456&password=123456";
        byte[] bs = Encoding.UTF8.GetBytes(form);
        request.ContentLength = bs.Length;
        using (Stream stream=request.GetRequestStream())
        {
            stream.Write(bs,0,bs.Length);
        }

        using (WebResponse response=request.GetResponse())
        {
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream, Encoding.UTF8);
            string buff = reader.ReadToEnd();
            Debug.Log(buff);
        }
    }

}
